package cn.rongdao.jrkabao.ui.financing.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.mifengkong.frtools.http.request.callback.BaseImpl;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.loadmore.OnScrollToBottomLoadMoreListener;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.financing.FinancingBean;
import cn.rongdao.jrkabao.model.financing.FinancingModel;
import cn.rongdao.jrkabao.ui.financing.adapter.FinancingAdapter;
import cn.rongdao.jrkabao.ui.financing.adapter.FinancingViewHolder;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.LoginUtil;

/**
 * Created by jack on 2017/9/8
 */

public class FinancingUIComponent extends LinearLayout implements FRBaseRecyclerAdapter.OnItemClickListener<FinancingViewHolder> {
    public FinancingUIComponent(Context context) {
        super(context);
    }

    public FinancingUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FinancingUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TitleBarUIComponent titleBarUIComponent;
    private PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    private FinancingAdapter financingAdapter;
    protected RecyclerAdapterWithHF mAdapter;
    private View viewFooter;

    private int page = 1;
    private int pageSize = 7;
    private int total = 10;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        titleBarUIComponent = (TitleBarUIComponent) findViewById(R.id.lf_titlebar);
        ptrRecyclerViewUIComponent = (PtrRecyclerViewUIComponent) findViewById(R.id.lf_ptrframelayout);
        viewFooter = FRView.inflateLayout(getContext(), R.layout.layout_financing_footer);

        initTitleBar();
    }

    public void initPtr(final BaseImpl baseImpl) {
        financingAdapter = new FinancingAdapter(getContext(), this);
        mAdapter = new RecyclerAdapterWithHF(financingAdapter);
        mAdapter.addFooter(viewFooter);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(getContext()));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);

        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getFinancingData(baseImpl, 1);
            }
        });

        ptrRecyclerViewUIComponent.setOnScrollToBottomLoadMoreListener(new OnScrollToBottomLoadMoreListener() {
            @Override
            public void onScrollToBottomLoadMore() {
                if (page < total) {
                    getFinancingData(baseImpl, ++page);
                    viewFooter.setVisibility(View.GONE);
                } else {
                    loadMoreComplete();
                    financingAdapter.setLastData();
                }
            }
        });
    }

    private void getFinancingData(final BaseImpl baseImpl, final int requestPage) {
        FinancingModel.getInstance().execute(FRString.valueOf(requestPage), FRString.valueOf(pageSize), new FRBaseObserver<FinancingBean>(baseImpl) {
            @Override
            protected void onBaseNext(FinancingBean data) {
                if (requestPage == 1) {
                    financingAdapter.setDataList(data.getList());
                    if (null != data.getList() && data.getList().size() >= pageSize && !ptrRecyclerViewUIComponent.isLoadMoreEnable()) {
                        ptrRecyclerViewUIComponent.setLoadMoreEnable(true);
                        viewFooter.setVisibility(View.GONE);
                    } else {
                        financingAdapter.setLastData();
                        loadMoreComplete();
                    }
                    ptrRecyclerViewUIComponent.refreshComplete();
                    page = requestPage;
                } else {
                    ptrRecyclerViewUIComponent.loadMoreComplete();
                    financingAdapter.appendList(data.getList());
                }
                mAdapter.notifyDataSetChanged();
                total = data.getTotal_pages();
                pageSize = data.getPage_size();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                if (page == 1) {
                    ptrRecyclerViewUIComponent.refreshComplete();
                } else {
                    loadMoreComplete();
                    financingAdapter.setLastData();
                }
            }
        });
    }

    private void loadMoreComplete() {
        viewFooter.setVisibility(View.VISIBLE);
        ptrRecyclerViewUIComponent.setLoadMoreEnable(false);
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        titleBarUIComponent.initTitle("什么值得投");
    }

    @Override
    public void onItemClick(FinancingViewHolder financingViewHolder, int position) {
        if (!LoginUtil.isLogin(getContext())) {
            return;
        }
        if (!FRString.isEmpty(financingAdapter.getDataList().get(position).getUrl())) {
            WebViewActivity.start(getContext(), financingAdapter.getDataList().get(position).getUrl());
        }
    }
}